<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<meta http-equiv="Cache-Control" content="no-siteapp">
		<link rel="stylesheet" href="../../../layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="../../../css/public.css" media="all" />
		<link rel="stylesheet" href="../../../plugin/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
	</head>

	<body class="childrenBody">
		<form class="layui-form" lay-filter="formTest" id="form" style="width: 80%;">
			<!-- <div class="layui-form-item layui-row layui-col-xs12"  > -->
			<div style="height: 300px; overflow: auto; margin-bottom: 50px">
				<ul id="ztree" class="ztree"></ul>
			</div>
			<div class="layui-form-item layui-row layui-col-xs12">
				<div class="layui-input-block">
					<button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="submit">提交</button>
					<button type="button" class="layui-btn layui-btn-sm layui-btn-primary close-btn">取消</button>
				</div>
			</div>
			<input name="id" id="id" type="hidden">
		</form>
		<script type="text/javascript" src="../../../js/jquery-2.1.1.min.js"></script>
		<script type="text/javascript" src="../../../layui/layui.js"></script>
		<script type="text/javascript" src="../../../custom/js/common.js"></script>
		<script type="text/javascript" src="../../../plugin/zTree/js/jquery.ztree.core.js"></script>
		<script type="text/javascript">
			layui.use(['form', 'layer'], function() {
				var form = layui.form;
				var layer = layui.layer;
				var toUpdateRoleApi = constant.baseUrl + "/system/user/toUpdateRole";
				var updateRoleApi = constant.baseUrl + "/system/user/updateRole";
				var setting = {
					data: {
						simpleData: {
							enable: true
						}
					}
				};
				var id = getUrlParam("id");
				if(id) {
					$("#id").val(id);
					loadAjax(toUpdateRoleApi, "get", null, function(response) {
						baseCallBack(response, function(response) {
							var zNodes = response.data;
							$.fn.zTree.init($("#ztree"), setting, zNodes);
						});
					});
				}
				form.on("submit(submit)", function(data) {
					//获取ztree 的js对象
					var treeObj = $.fn.zTree.getZTreeObj("ztree");
					var nodes = treeObj.getSelectedNodes();
					//拿到ztree选中的节点
					if(nodes && nodes.length) {
						for(var i = 0; i < nodes.length; i++) {
							var node = nodes[i];
							//动态拼接到form表单里面
							$("#form").append("<input type='hidden' name='rid' value='" + node.id + "' />");
						}
						form.render();
						loadAjax(updateRoleApi, "put", $("#form").serialize(), function(response) {
							baseCallBack(response, parentReloadCallBack);
						});
					} else {
						layer.msg("请选择岗位");
					}

					return false;
				})
			})
		</script>
	</body>

</html>